<template>
    <div class="goods-list">
        <router-link class="goods-item" v-for="item in goodsList" :key="item.id" :to="`/home/goodsInfo/${item.id}`" tag="div">
            <img :src="item.img_url">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span class="important">热卖中</span>
                    <span>剩余<span class="important">{{item.stock_quantity}}</span>件</span>
                </p>
            </div>
        </router-link>
        <mt-button type="primary" size="large" plain @click="getMoreGoods">加载更多</mt-button>
    </div>
</template>

<script>
    export default {
        name: "GoodsList",
        data() {
            return {
                pageIndex: 1, //分页页数
                goodsList: [],
            }
        },
        created() {
            this.getGoodsList();
        },
        methods: {
            getGoodsList() {//获取商品列表数据
                this.axios.get(`getgoods?pageindex=${this.pageIndex}`)
                    .then(response => {
                        if (response.data.status === 0) {
                            this.goodsList = this.goodsList.concat(response.data.message);
                        } else {
                            this.$toast('获取商品列表失败!');
                        }
                    })
            },
            getMoreGoods() {
                this.pageIndex++;
                this.getGoodsList();
            }
        }
    }
</script>

<style scoped>
    .goods-list {
        display: flex;
        flex-wrap: wrap;
        padding: 10px;
        justify-content: space-between;
    }
    .goods-item {
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 4px 0;
        padding: 4px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
    .goods-item img {
        width: 100%;
    }
    .info {
        background-color: #eee;
    }
    .info p {
        margin: 0;
        padding: 10px;
    }
    .price {

    }
    .now {
        color: red;
        font-weight: bold;
        font-size: 40px;
    }
    .old {
        text-decoration: line-through;
        font-size: 30px;
        margin-left: 10px;
    }
    .sell {
        display: flex;
        justify-content: space-between;
        font-size: 30px;
    }
    .mint-button--large {
        height: 80px;
        font-size: 30px;
    }
    .important {
        color: red;
    }
</style>